function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function getDateDiff(dateTimeStamp) {
  var minute = 1000 * 60;
  var hour = minute * 60;
  var day = hour * 24;
  var halfamonth = day * 15;
  var month = day * 30;
  var year = day * 365;
  var now = new Date().getTime();
  var diffValue = now - dateTimeStamp;
  if (diffValue < 0) {
    //非法操作
    return '数据出错';
  }
  var yearC = diffValue / year;
  var monthC = diffValue / month;
  var weekC = diffValue / (7 * day);
  var dayC = diffValue / day;
  var hourC = diffValue / hour;
  var minC = diffValue / minute;
  if (yearC >= 1) {
    result = parseInt(yearC) + '年以前';
  } else if (monthC >= 1) {
    result = parseInt(monthC) + '个月前';
  } else if (weekC >= 1) {
    result = parseInt(weekC) + '星期前';
  } else if (dayC >= 1) {
    result = parseInt(dayC) + '天前';
  } else if (hourC >= 1) {
    result = parseInt(hourC) + '小时前';
  } else if (minC >= 5) {
    result = parseInt(minC) + '分钟前';
  } else {
    result = '刚刚发表';
  }
  return result;
}

//友好时间
var build_time_friendly = function (time) {
  if (time == '') return;
  var now = new Date().getTime();
  time = Math.round(now / 1000) - time;
  if (time <= 0) return '刚刚';
  var times = '';
  var years = Math.floor(time / 31104000);
  var months = Math.floor((time / 2592000) % 12);
  var days = Math.floor((time / 86400) % 30);
  var hours = Math.floor((time / 3600) % 24);
  var minutes = Math.floor((time / 60) % 60);
  time = Math.floor(time % 60);
  if (time >= 1) times = time + '秒';
  if (minutes >= 1) times = minutes + '分钟';
  if (hours >= 1) times = hours + '小时';
  if (days >= 1) times = days + '天';
  if (months >= 1) times = months + '月';
  if (years >= 1) times = years + '年';
  times += '前';
  return times;
}

//友好时间  m/d
var build_time = function (time) {
  if (time == '') return;

  return formatDate('m/d', time * 1000);
}

//友好时间  m/d
var build_time_ymd = function (time) {
  if (time == '') return;

  return formatDate('y-m-d', time * 1000);
}

//formatDate('y/m/d', 2132132131) 某时间格式为 年月日 如 2010/12/5 
function formatDate(formatStr, fdate) {
  var fTime, fStr = 'ymdhis';
  if (!formatStr)
    formatStr = "y-m-d h:i:s";
  if (fdate)
    fTime = new Date(fdate);
  else
    fTime = new Date();
  var formatArr = [
    fTime.getFullYear().toString(),
    (fTime.getMonth() + 1).toString(),
    fTime.getDate().toString(),
    fTime.getHours().toString(),
    fTime.getMinutes().toString(),
    fTime.getSeconds().toString()
  ]
  for (var i = 0; i < formatArr.length; i++) {
    formatStr = formatStr.replace(fStr.charAt(i), formatArr[i]);
  }
  return formatStr;
}

function imageUtil(e) {
  var imageSize = {};
  var originalWidth = e.detail.width;//图片原始宽 
  var originalHeight = e.detail.height;//图片原始高 
  var originalScale = originalHeight / originalWidth;//图片高宽比 
  console.log('originalWidth: ' + originalWidth)
  console.log('originalHeight: ' + originalHeight)
  //获取屏幕宽高 
  wx.getSystemInfo({
    success: function (res) {
      var windowWidth = res.windowWidth;
      var windowHeight = res.windowHeight;
      var windowscale = windowHeight / windowWidth;//屏幕高宽比 
      console.log('windowWidth: ' + windowWidth)
      console.log('windowHeight: ' + windowHeight)
      if (originalScale < windowscale) {//图片高宽比小于屏幕高宽比 
        //图片缩放后的宽为屏幕宽 
        imageSize.imageWidth = windowWidth;
        imageSize.imageHeight = (windowWidth * originalHeight) / originalWidth;
      } else {//图片高宽比大于屏幕高宽比 
        //图片缩放后的高为屏幕高 
        imageSize.imageHeight = windowHeight;
        imageSize.imageWidth = (windowHeight * originalWidth) / originalHeight;
      }

    }
  })
  console.log('缩放后的宽: ' + imageSize.imageWidth)
  console.log('缩放后的高: ' + imageSize.imageHeight)
  return imageSize;
}

//去除html标签
function removeHtmlTags(str) {
  str = str.replace(/<\/?[^>]*>/g, ''); //去除HTML tag
  str = str.replace(/[ | ]*\n/g, '\n'); //去除行尾空白
  //str = str.replace(/\n[\s| | ]*\r/g,'\n'); //去除多余空行
  str = str.replace(/&nbsp;/ig, '');//去掉&nbsp;
  return str;
}


// 手机号登录
function mobileLogin() {
  var id = 1;
  var url = '../login/login?id=' + id;
  console.log('跳转登录' + id);
  wx.navigateTo({
    url: url
  })
}

//获取用户uid
var uid = 0;
function getUserId() {
  //根据登录状态
  if (wx.getStorageSync('logined')) {
    if (uid == 0) {
      var user = wx.getStorageSync('user');
      uid = user.uid;
    }
  }
  else {
    uid = 0;
  }
  return uid;
}

function getUser() {
  var user = wx.getStorageSync('user');
  return user;
}

// 检查用户是否已登录
function checkLogin(winname, loginType) {
  var logined = wx.getStorageSync('logined');
  console.log("checkLogin-" + logined + " - " + getUserId());
  if (logined && getUserId()) {
    return true;
  } else {
    switch (loginType || '') {
      case 'phone':
        mobileLogin();
        break;
      default:
        openWin_Login(winname);
    }
    return false;
  }
}

function getShareAppMessage() {
  return {
    title: '抢试用-瑞丽网',
    desc: '瑞丽网试用中心是中国最大的化妆品免费试用中心，整合了800多个化妆品品牌3.7万款热门产品进行试用，为消费者提供购买参考。',
    path: '/pages/trials/trials'
  };
}

module.exports = {
  formatTime: formatTime,
  getDateDiff: getDateDiff,
  build_time: build_time,
  build_time_ymd: build_time_ymd,
  imageUtil: imageUtil,
  removeHtmlTags: removeHtmlTags,
  checkLogin: checkLogin,
  getUserId: getUserId,
  getUser: getUser,
  getShareAppMessage: getShareAppMessage
}
